﻿using AutoMapper;
using Enter.Admin.Entities;
using Enter.Admin.Enums;
using Enter.Admin.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Enter.Admin.Controllers
{
    public class BaseController : Controller
    {
        protected readonly IFreeSql _fsql;
        protected readonly IMapper _mapper;
        protected readonly ILogger _logger;

        public BaseController(ILogger<BaseController> logger, IFreeSql fsql, IMapper mapper)
        {
            _logger = logger;
            _fsql = fsql;
            _mapper = mapper;
        }

        protected void UpdateSession(long id = 0)
        {
            var session = HttpContext.Session.Get<Employ>();
            if (session != default)
            {
                if (id == 0) id = session.Id;
                if (id == session.Id)
                {
                    var user = _fsql.Select<Employ>().Include(t => t.Role).Where(t => t.Id == session.Id).First();
                    HttpContext.Session.Set<Employ>(user);
                }
            }
        }

        public static void UpdateEnumDictionary(IFreeSql _fsql)
        {
            var enum_group = _fsql.Select<EnumDictionary>().ToList().GroupBy(t => t.DicType);
            foreach (var enumOne in enum_group) 
            {
                var key = enumOne.Key;
                var items = new List<SelectListItem>();
                foreach (var g in enumOne.OrderBy(t=>t.Sort)) 
                {
                    items.Add(new SelectListItem { Value = g.Value, Text = g.Text });
                }
                EnumsExt.EnumMap.AddOrUpdate(key, items, (oldkey, oldvalue) => items);
            }
        }

        public static void UpdateUserList(IFreeSql _fsql)
        {
            var users = _fsql.Select<Employ>().Where(t => t.IsDeleted == false).ToList()
            .Select(t => new SelectListItem { Value = t.Id.ToString(), Text = t.TrueName }).ToList();
            EnumsExt.EnumMap.AddOrUpdate(nameof(EnumArchive.EnumEmploy), users, (oldkey, oldvalue) => users);
        }

        public static void UpdateDynamicTableList(IFreeSql _fsql)
        {
            var tables = _fsql.Select<DynamicTable>().ToList()
            .Select(t => new SelectListItem { Value = t.Id.ToString(), Text = t.Name }).ToList();
            EnumsExt.EnumMap.AddOrUpdate(nameof(EnumArchive.EnumDynamicTable), tables, (oldkey, oldvalue) => tables);
        }

        public static void Init(IFreeSql _fsql, IConfiguration configuration)
        {
            // 初始化数据库配置;
            var settings = _fsql.Select<Setting>().ToList();
            foreach (var setting in settings)
            {
                configuration[setting.Key] = setting.Value;
            }
            // 初始化用户;
            UpdateUserList(_fsql);
            // 初始化枚举;
            UpdateEnumDictionary(_fsql);
            // 初始化所属机构;
            UpdateDynamicTableList(_fsql);
        }

        protected void SetSession(Employ session)
        {
            HttpContext.Session.Set<Employ>(session);
        }

        protected Employ Session()
        {
            return HttpContext.Session.Get<Employ>();
        }
    }
}
